
ac.game:event '单位-吞噬物品'(function(_,unit,item)
    local player = unit.owner
    local hero = player.hero
    local lock = player:get_data('不可吞噬') or {}
    local list = player:get_data('装备吞噬列表') or {}
    if not hero then
        return false
    end
    if lock[item.name] then
        return false
    end
    lock[item.name] = true
    show_item_tip(player,item,'吞噬')
    table.insert(list,item)
    table.sort(list,function(a,b) return a.item_level>b.item_level end)
    item:remove()
    item:notify('on_absorb',hero)
    player:set_data('不可吞噬', lock)
    player:set_data('装备吞噬列表', list)
    hero:notify('单位-添加物品属性',hero,item)
    hero:notify('单位-吞噬物品完成',hero,item)

    for _,it in ipairs(hero:get_items()) do
        if it~=item and it.name==item.name and it.item_level>=6 then
            it:discard()
        end
    end
end)



ac.game:event '单位-准备吞噬物品'(function(_,hero,item)
    local gold = item.tzjb
    if gold then
        local player = hero.owner
        local lock = player:get_data('不可吞噬') or {}
        if lock[item.name] then
            return false
        end
        if player:get('金币')<gold then
            show_item_tip(player,item,'金币')
            return
        end
        if hero:dispatch('单位-吞噬物品', hero, item)==false then
            ac.console:error(player, "该装备已被吞噬")
        else
            player:add('金币',-gold)
        end
    end
end)

ac.sync:event '同步-吞噬物品'(function(_,player,handle,index)
    local unit = ac.unit(handle)
    if unit then
        for item in unit:search_items() do
            if item.slotid == index then
                unit:notify('单位-准备吞噬物品',unit,item)
                return
            end
        end
    end
end)

ac.console:event '控制台-点击物品按钮'(function(_,index)
    local unit = ac.unit.localunit()
    ac.sync('同步-吞噬物品',unit.handle,index)
end)


ac.item('六级吞噬丹'){
    item_type = "charged",
    auto_merge = true,
    targettype = "物体",
    allowtype = '物品',
    count = 1,
    range = 300,
    cool = 0.2,
    gold = 10000,
    art = [[图标\wp_6jtsd.blp]],
    model = [[TSD_6J.mdl]],
    tip = [[吞噬一件6级以上的装备，获得该装备的属性]],
    on_use = function(self, item)
        local hero = self.owner
        local player = hero.owner
        local lv = item.item_level or 0
        if item['无法吞噬'] then
            self:set_count(self:get_count())
            ac.console:error(player, "该装备无法吞噬")
            return
        end
        if lv~=6 then
            self:set_count(self:get_count())
            ac.console:error(player, "只能对6级装备使用")
            return
        end
        if hero:dispatch('单位-吞噬物品', hero, item)==false then
            self:set_count(self:get_count())
            ac.console:error(player, "该装备已被吞噬")
        end
    end
}

ac.item('七级吞噬丹'){
    item_type = "charged",
    targettype = "物体",
    allowtype = '物品',
    auto_merge = true,
    count = 1,
    range = 300,
    cool = 0.2,
    gold = 30000,
    model = [[TSD_7J.mdl]],
    tip = [[吞噬一件6级以上的装备，获得该装备的属性]],
    on_use = function(self, item)
        local hero = self.owner
        local player = hero.owner
        local lv = item.item_level or 0
        if lv~=7 then
            self:set_count(self:get_count())
            ac.console:error(player, "只能对7级装备使用")
            return
        end
        if hero:dispatch('单位-吞噬物品', hero, item)==false then
            self:set_count(self:get_count())
            ac.console:error(player, "该装备已被吞噬")
        end
    end
}
